/**
 * Created by pankajmishra on 21/01/15.
 */
define(['jquery'], function($){

    var myInterval = window.setInterval(timedMousePos,250);
    // Variables for mouse positions
    var xPos = -1;
    var yPos = -1;
    var firstX = -1;
    var firstY = -1;
// variable to track how many times I polled the mouse position
    var intervals = 0;


    function calcDistance(aX,aY,bX,bY) {
        //use the Euclidean 2 dimensional distance formula to calculate the distance
        //in pizels between the coordinate sets
        var mouseTraveled = Math.round(Math.sqrt(Math.pow(aX-bX,2)+Math.pow(aY-bY,2)));
        //ajax stuff to set a session variable
        try	{
            // Dave Shuck - 26 Mar 2007 - added try/catch for giant pages that take a while to
            // load, in case the user moves their mouse before the page is completely rendered.
            // Bas van der Graaf (bvdgraaf@e-dynamics.nl)
            // Get all usedKeyboard inputs and set value
            var elMouseMovement = $('.bb_sec1');
            for (var i=0;i<elMouseMovement.length;i++)
            {
                elMouseMovement[i].value = mouseTraveled
            }
        }
        catch(excpt) { /* no action to take */ }
    }

    // capture mouse movement
    function timedMousePos() {

        $(document).once('mousemove', function(e){
            xPos=event.pageX;
            yPos=event.pageY;
            if (xPos >= 0 && yPos >= 0) {
                //0,0 is a valid mouse position, so I want to accept that.  for this reason
                //my vars are initialized to -1
                var newX = xPos;
                var newY = yPos;
                intervals++;
            }
            if (intervals == 1) {
                //store the first coordinates when we've got a pair (not when 'intervals' is 0)
                firstX = xPos;
                firstY = yPos;
            } else if (intervals == 2) {
                //I've got two coordinate sets
                //tell the browser to stop executing the timedMousePos function
                clearInterval(myInterval);
                //send the 4 mouse coordinates to the calcDistance function
                calcDistance(firstX,firstY,newX,newY);
            }
        });
    }

    var keysPressed = 0;
//capture when a user uses types on their keyboard
    $(document).once('keydown',logKeys);

    function logKeys() {
        //user hit a key, increment the counter
        keysPressed++;
        // Bas van der Graaf (bvdgraaf@e-dynamics.nl)
        // Get all usedKeyboard inputs and set value
        var elKeyPressed = $('.bb_sec2');
        for (var i=0;i<elKeyPressed.length;i++)
        {
            elKeyPressed[i].value = keysPressed
        }
    }


});
